package com.sgb.contract.view.ui.fragment;

import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sgb.contract.R;
import com.sgb.contract.databinding.ContractUpdateFragmentBinding;
import com.sgb.contract.model.UpdateBean;
import com.sgb.contract.view.ui.details.OtherInfoActivity;
import com.sgb.contract.view.ui.details.UpdateAdapter;
import com.swgk.core.base.AppLazyFragment;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

/**
 * 作者:张磊
 * 日期:2021/11/29 0029
 * 说明:修改记录
 */
public class UpdateFragment extends AppLazyFragment {

    private ContractUpdateFragmentBinding mBinding;
    private UpdateViewModel mModel;
    private UpdateAdapter mAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.contract_update_fragment, container, false);
        return mBinding.getRoot();
    }


    @Override
    protected void initView() {
        super.initView();
        mModel = new ViewModelProvider(this).get(UpdateViewModel.class);
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onLazyLoad() {
        mAdapter = new UpdateAdapter(new ArrayList<>());
        List<UpdateBean> beans = new ArrayList<>();
        mBinding.rv.setLayoutManager(new LinearLayoutManager(getActivity()));
        mBinding.rv.setAdapter(mAdapter);
        mAdapter.setNewData(beans);

        mAdapter.setOnItemChildClickListener((adapter, v, position) -> {
            if (v.getId() == R.id.ll_btn) {
                OtherInfoActivity activity = (OtherInfoActivity) getActivity();
                activity.show(mBinding.rv, mAdapter.getData().get(position));
            }
        });

        mModel.findByContractNo(getActivity().getIntent().getStringExtra("contractNo"));
        mModel.mDatas.observe(this, data ->
                {
                    if (data == null || data.size() == 0) {
                        mBinding.rlNull.setVisibility(View.VISIBLE);
                        mBinding.rv.setVisibility(View.GONE);
                    }
                    mAdapter.setNewData(data);
                }
        );

    }

}